package com.stephenone.zhdaily.storydetail;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;

import com.stephenone.zhdaily.Constants;
import com.stephenone.zhdaily.R;
import com.stephenone.zhdaily.comment.CommentActivity;
import com.stephenone.zhdaily.data.bean.NewsDetail;

public class StoryDetailFragment extends Fragment implements StoryDetailContract.View {
    private StoryDetailContract.Presenter mPresenter;
//    private TextView mTextView;
    private WebView mWebView;
    private long mStoryId;

    public StoryDetailFragment() {
    }

    public static StoryDetailFragment newInstance(long id) {
        StoryDetailFragment fragment = new StoryDetailFragment();
        Bundle args = new Bundle();
        args.putLong(Constants.KEY_STORY_ID,id);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mStoryId = getArguments().getLong(Constants.KEY_STORY_ID);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View contentView = inflater.inflate(R.layout.fragment_story_detail, container, false);
//        mTextView = contentView.findViewById(R.id.tv_content);

        // In your fragment's onCreateView() method, you need to call setHasOptionsMenu(true) to tell the host activity that your fragment has menu options that it wants to add.
        setHasOptionsMenu(true);

        mWebView = contentView.findViewById(R.id.webview);
        initWebView();
        return contentView;
    }

    private void initWebView() {
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        // 开启DOM storage API 功能
        mWebView.getSettings().setDomStorageEnabled(true);
        // 开启database storage API功能
        mWebView.getSettings().setDatabaseEnabled(true);
        // 开启Application Cache功能
        mWebView.getSettings().setAppCacheEnabled(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_story_detail,menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_commnet:
//                mPresenter.deleteTask();
                Intent intent = new Intent(getActivity(), CommentActivity.class);
                intent.putExtra(Constants.KEY_STORY_ID,mStoryId);
                getActivity().startActivity(intent);
                return true;
        }
        return false;
    }


    @Override
    public void onResume() {
        super.onResume();
        mPresenter.subscribe();
    }

    @Override
    public void onPause() {
        mPresenter.unsubscribe();
        super.onPause();
    }

    @Override
    public void setPresenter(StoryDetailContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void showNewsDetail(NewsDetail newsDetail) {

//        mTextView.setText(newsDetail.getBody());

        String css = "<link rel=\"stylesheet\" href=\""+ newsDetail.getCss().get(0) +"\" type=\"text/css\">";
//        String css = newsDetail.getCss().get(0);
        String html = "<html><head>" + css + "</head><body>" + newsDetail.getBody() + "</body></html>";
        html = html.replace("<div class=\"img-place-holder\">", "");
        mWebView.loadDataWithBaseURL("x-data://base", html, "text/html", "UTF-8", null);
    }
}
